package com.zhixiang.xaec.action.system;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zhixiang.core.command.QueryFilter;
import com.zhixiang.core.util.ContextUtil;
import com.zhixiang.core.util.String2Array;
import com.zhixiang.core.util.StringUtil;
import com.zhixiang.core.web.action.BaseAction;
import com.zhixiang.xaec.entity.system.AppUser;
import com.zhixiang.xaec.service.system.AppUserService;


@Controller
@Scope("prototype")
public class AppUserAction extends BaseAction
{

    private static final long serialVersionUID = -5082374994337872960L;

    private AppUser appUser;

    @Autowired
    private AppUserService appUserService;

    public String save()
    {
        // Gson localGson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String msg = appUserService.saveUser(appUser);
        if (msg != null)
        {
            setSuccess(false);
            setMsg(msg);
        }
        return SUCCESS;
    }

    public String get()
    {
        if (appUser != null && StringUtils.isNotEmpty(appUser.getUserId().toString()))
        {
            setResult(appUserService.get(appUser.getUserId()));
        }
        return SUCCESS;
    }

    public String delete()
    {
        try
        {
            String ids = getRequest().getParameter("ids");
            Long[] l = String2Array.stringToLongArray(ids);
            appUserService.remove(l);
        }
        catch (Exception e)
        {
            setSuccess(false);
            setMsg("删除用户失败");
        }
        return SUCCESS;
    }

    public String list()
    {
        QueryFilter localQueryFilter = new QueryFilter(getRequest());
        List<AppUser> localList = appUserService.getAll(localQueryFilter);
        setSuccess(true);
        setTotalCounts(localQueryFilter.getPagingBean().getTotalItems());
        setResult(localList);
        return SUCCESS;
    }

    public String createPassword()
    {
        String str2 = getRequest().getParameter("newpassword");
        String str3 = getRequest().getParameter("password");
        if (StringUtils.isNotEmpty(appUser.getUserId().toString()))
        {
            setAppUser(appUserService.get(appUser.getUserId()));
        }
        else
        {
            setAppUser(ContextUtil.getCurrentUser());
        }
        if (str2.equals(str3))
        {
            appUser.setPassword(StringUtil.encryptSha256(str2));
            appUserService.save(this.appUser);
        }
        else
        {
            setSuccess(false);
            setMsg("重置失败!两次输入的密码不一致,请重新输入!");
        }
        return SUCCESS;
    }

    public String resetPassword()
    {
        String str1 = getRequest().getParameter("appUserUserId");
        String str2 = StringUtil.encryptSha256(getRequest().getParameter("oldPassword"));
        String str3 = getRequest().getParameter("newPassword");
        String str4 = getRequest().getParameter("againPassword");
        if (StringUtils.isNotEmpty(str1)){
            setAppUser(appUserService.get(new Long(str1)));
        }
        else{
            setAppUser(ContextUtil.getCurrentUser());
        }
        if (str2.equals(appUser.getPassword()))
        {
            if (str3.equals(str4)){
                appUser.setPassword(StringUtil.encryptSha256(str3));
                appUserService.save(appUser);
            }
            else{
                setSuccess(false);
                setMsg("两次输入不一致.");
            }
        }
        else{
            setSuccess(false);
            setMsg("旧密码输入不正确.");
        }
       
        return SUCCESS;
    }

    public AppUser getAppUser()
    {
        return appUser;
    }

    public void setAppUser(AppUser appUser)
    {
        this.appUser = appUser;
    }

}
